use std::error::Error;

pub type HuffmanResult<T> = Result<T,HuffmanError>;

#[derive(Debug)]
pub enum HuffmanError {
    Byte(crate::byte::ByteError),
    Fse(crate::fse::FseError),
    Tree,
}

impl std::fmt::Display for HuffmanError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Byte(e) => write!(f, "[huffman]{e}"),
            Self::Fse(e) => write!(f, "[huffman]{e}"),
            Self::Tree => write!(f, "[huffman]tree build error"),
        }
    }
}

impl Error for HuffmanError { }

impl From<crate::byte::ByteError> for HuffmanError {
    fn from(value: crate::byte::ByteError) -> Self {
        Self::Byte(value)
    }
}

impl From<crate::fse::FseError> for HuffmanError {
    fn from(value: crate::fse::FseError) -> Self {
        Self::Fse(value)
    }
}